#include "http.h"
#include "json.h"
#include <fstream>
#include <iostream>

int main(int argc, char **argv) { 
    if(argc !=2){
        printf("Usage: ./activate_client activate_code_directory \n");
        return -1;
    }
    std::cout << "activate client is running !" << std::endl;

    bool status = true;

    std::string url = "120.77.254.27:5002";
    httplib::Client cli(url);
    cli.set_connection_timeout(10, 0);

    while(status){
        //1.获取uuid


        //2.发送uuid
        std::string json_body = "uuid=changan123456789&sn=5227f8ee&version=v1.0.0&projectNum=c857&ai=dms";

        httplib::Result res = cli.Post("/recordUuidMode/activate", json_body, "application/x-www-form-urlencoded");
        std::cout << "--------------------------------" << std::endl;
        std::cout << res->status << std::endl;
        std::cout << res->body << std::endl;
        if(res && res->status == 200){
            std::string response = res->body;
            //这里判断响应状态码，如果成功，则进行下一步
            Json::Value root;
            Json::Reader jsonReader;
            if (!jsonReader.parse(response, root)) {
                std::cout << "parse json error!" << std::endl;

                continue;
            }

            if (root["code"].asString() == "F001") {
                std::cout << "code: " << root["code"].asString() << std::endl;

                continue;
            } else if (root["code"].asString() == "F002") {
                std::cout << "code: " << root["code"].asString() << std::endl;

                continue;
            } else if (root["code"].asString() == "F003") {
                std::cout << "code: " << root["code"].asString() << std::endl;

                continue;
            } else if (root["code"].asString() == "F004") {
                std::cout << "code: " << root["code"].asString() << std::endl;

                continue;
            } else if (root["code"].asString() == "F005") {
                std::cout << "code: " << root["code"].asString() << std::endl;
                continue;
            } else if (root["code"].asString() == "F006") {
                std::cout << "code: " << root["code"].asString() << std::endl;
                continue;
            } else if (root["code"].asString() == "F007") {
                std::cout << "code: " << root["code"].asString() << std::endl;
                continue;
            } else if (root["code"].asString() == "F008") {
                std::cout << "code: " << root["code"].asString() << std::endl;
                continue;
            }else if (root["code"].asString() == "SYSERR") {
                std::cout << "code: " << root["code"].asString() << std::endl;
                continue;
            }


            //3、保存激活码到激活文件
            std::string activate_code_file = argv[1];
            std::string activate_code = root["ActivationCode"].asString();
            std::ofstream outFile;             // 定义ofstream对象outFile
            outFile.open(activate_code_file);  // 打开文件
            outFile << activate_code;          // 写入
            outFile.close();                   // 关闭文件

            status = false; //激活成功则停止激活程序
        }else{
            std::cout << to_string(res.error()) << std::endl;
        }
        
        
        sleep(1);
    }

    std::cout << "activate client is stop !" << std::endl;

}